const httpclient = require('../../../utils/httpclient.js')
const md5Util = require('../../../utils/md5.js')
const util = require('../../../utils/util.js')

var app = getApp()
Page({
  data: {
    statusType: [{
      state: "",
      word: "全部"
    }, {
      state: "2",
      word: "待派送"
    }, {
      state: "3",
      word: "派送中"
    }, {
      state: "4",
      word: "已完成"
    }],
    server: app.globalData.server,
    currentTpye: "",
    tabClass: ["", "", "", ""],
    nextStateMassage: {
      2: "马上发货",
      3: "交易完成",
      4: ""
    },
    pageNumber: 1,
    pageSize: 10,
    orderList: [],
    hidden: false,
    isRefresh: false,
    page: {
      pagenum: 1
    }
  },
  statusTap: function (e) {
    var curType = e.currentTarget.dataset.index;
    if (curType != this.data.currentTpye) {
      this.setData({
        currentTpye: curType,
        pageNumber: 1,
        orderList: []
      });
      this.onShow();
    }
  },
  refresh: function (e) {
    console.log(e);
    console.log('开始刷新!');
    wx.stopPullDownRefresh();
    this.setData({
      isRefresh: true
    });
  },
  loadMore: function () {
    // 如果没有到最后一页才加载新的数据
    if (this.data.page.pageNumber < this.data.page.pageCount) {
      this.setData({ pageNumber: this.data.pageNumber + 1 });
      this.getDataFromServer(this.data.pageNumber);
    }
  },
  onLoad: function (options) {
    // 生命周期函数--监听页面加载
    var that = this;
    /** 
     * 获取系统信息 
     */
    wx.getSystemInfo({
      success: function (res) {
        var n = 750 / res.windowWidth;
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
          scrollHeight: res.windowHeight - (89 / n),
          upperThreshold: 80 / n
        });
      }
    });
  },
  updateState: function (e) {
    let eleData = e.currentTarget.dataset;
    let that = this;
    httpclient.req(
      "/manage/order/update",
      {
        id: eleData.id,
        state: Number(eleData.state) + 1
      },
      'post',
      function (res) {
        console.log(res);
        var result = res.data.result;
        if (res.data.code == 0){
          wx.showToast({
            title: '操作成功',
            icon: 'success',
            duration: 2000
          });
          that.setData({
            pageNumber: 1,
            orderList: []
          });
          that.onShow();
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'success',
            duration: 2000
          });
        }
      },
      function (result) {
        console.log(result)
      }
    );
  },
  onShow: function () {
    // 获取订单列表
    var that = this;
    that.setData({
      orderList: []
    });
    that.getDataFromServer(1);
  },
  //获取网络数据的方法
  getDataFromServer: function (pageNumber) {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    var that = this;
    this.setData({
      hidden: false,
      hasMore: true
    });
    var postData = {
      pageNumber: pageNumber,
      office_id: app.globalData.office_id
    }
    if (that.data.currentTpy != "") {
      postData.state = that.data.currentTpye;
    }

    httpclient.req(
      "/manage/order/list",
      postData,
      'post',
      function (res) {
        var orderList = that.data.orderList.concat(res.data.result.list);
        if (orderList.length == 0) {
          that.setData({ hiddenNoCoupon: false });
        } else {
          that.setData({ hiddenNoCoupon: true });
        }
        that.setData({
          orderList: orderList,
          hidden: true,
          page: res.data.result.page,
          hasMore: false
        })
        wx.hideNavigationBarLoading() //完成停止加载
      },
      function (result) {
        console.log(result)
      }
    );
  },
  releaseLoad: function (e) {
    console.log('开始释放');
    // 如果是刷新状态才有效
    if (this.data.isRefresh) {
      // 取得当前的pageY
      var pageY = e.changedTouches[0].pageY;
      var diffeY = pageY - this.data.pageY;
      if (diffeY > 60) {
        var that = this;
        if (that.data.hidden) {
          that.setData({
            pageNumber: 1,
            orderList: []
          });
          that.onShow();
        }
      }
      this.setData({
        isRefresh: false
      });
    }
  },
  scrollStart: function (e) {
    console.log('触摸开始');
    // 记录开始时候触摸时候移动的y轴距离
    this.setData({
      pageY: e.changedTouches[0].pageY
    });
  }
})